<?php
/**
 * admin
 * @version 1.0
 * @package application
 * @subpackage application/controllers/admin/
 */
class Admin extends MY_Controller
{	

	private $_rule_config=array(
		array('field'=> 'group_id' , 'label'=> '管理员组' , 'rules'=> 'required'),
		array('field'=> 'user_name' , 'label'=> '姓名' , 'rules'=> 'required'),
		array('field'=> 'real_name' , 'label'=> '真实姓名' , 'rules'=> 'required'),
			array('field'=> 'real_name' , 'label'=> '密码和确认密码必须一致' , 'rules'=> 'password', 'repassword', 'required|matches[passconf]'),
				
	);

	public function __construct(){
		parent::__construct();		
		$this->load->model('admin_model');
		$this->load->model('admin_group_model');
	}
	/**
	 * index 显示列表页
	 * @author 
	 */	
	public function index(){
		$attributes=$this->input->get();
		$search['attributes']=$attributes;
		// 总数与分页
		$total = $this->admin_model->total($search);
		$data['total'] = $total;
		// 请使用config_item( 'per_page' )获取全局显示条数
		$per_page = config_item( 'per_page' );
		$this->load->library('pagination');
		$pagination_config = array(
			'base_url' => base_url($this->_site_path.'/admin/index' ),
			'total_rows' => $total,
			'per_page' => $per_page,
			'uri_segment' => 4,
		);
		$this->pagination->initialize( $pagination_config );
		$data['pagination'] = $this->pagination->create_links();

		$search['limit']=array('persize'=>$per_page,'offset'=>$this->pagination->get_cur_offset());
		$data['admin_data']=$this->admin_model->all($search);
		$this->load->view($this->_site_path.'/admin/index',$data);
	}

	/**
	 * view 显示详细页
	 * @param $id 主键值
	 * @author 
	 */
	public function view($id)
	{
		$data['admin_data']=$this->admin_model->get_by_pk($id);
		$this->load->view($this->_site_path.'/admin/view',$data);
	}


	/**
	 * 初始化数据
	 *by  dengle 
	 */

	private function _init_data( &$data ) {
		// 获取管理员组
		$data['group'] = $this->admin_group_model->get_values('group_id','group_name',array('state'=>'1'));

	}

	/**
	 * add 添加 
	 * @author 
	 */
	public function add()
	{
		$data = array( );
		$data['is_edit'] = FALSE;
		if ( $this->input->post( 'is_submit' ) && $this->form_validation->check_token() ){
			try {
				$admin_data = $this->input->post();
				$data['admin_data'] = $admin_data;
				//设置验证规则
				$this->form_validation->set_rules($this->_rule_config);
				if ( !$this->form_validation->run() ) throw new Exception( validation_errors(), 0 );
				
				$admin_data['created'] = time();
				if($this->input->post('password') || $this->input->post('repassword')){
					$admin_data['password'] = md5( $this->input->post('password') );
					$admin_data['repassword'] = md5( $this->input->post('repassword') );
					if( $admin_data['repassword'] != $admin_data['password'] )  throw new Exception( '两次密码不一致', -1 );
				}
				if($this->input->post('begin_time')){
					$admin_data['begin_time'] = strtotime($this->input->post('begin_time'));
				}
				if($this->input->post('begin_time')){
					$admin_data['end_time'] = strtotime($this->input->post('begin_time'));
				}
				//$admin_data['password'] = md5( $admin_data['password'] );
				//$admin_data['begin_time'] = strtotime($admin_data['begin_time'] );
				//$admin_data['end_time'] = strtotime($admin_data['end_time'] );
				//插入
				$admin_id = $this->admin_model->insert( $admin_data );
				init_messagebox( '添加成功', 'success', 1, base_url( $this->_site_path.'/admin/index' ) );
			} catch ( Exception $e ) {
				init_messagebox( $e->getMessage(), 'error', $e->getCode() );
			}
		}
		$this->_init_data( $data );
		$this->load->view($this->_site_path.'/admin/form', $data );
	}

	/**
	 * edit 编辑 
	 * @param $id 主键值
	 * @author 
	 */
	public function edit($id)
	{
		$data = array( );
		$data['is_edit']  = TRUE;
		$data['admin_data']=$this->admin_model->get_by_pk($id);
		if ( $this->input->post( 'is_submit' ) && $this->form_validation->check_token() ){
			try {
				$admin_data = $this->input->post();
				$data['admin_data'] = $admin_data;
				//设置验证规则
				$this->form_validation->set_rules($this->_rule_config);
				if ( !$this->form_validation->run() ) throw new Exception( validation_errors(), 0 );
				
				$admin_data['modified'] = time();
				if($this->input->post('password') || $this->input->post('repassword')){
					$admin_data['password'] = md5( $this->input->post('password') );
					$admin_data['repassword'] = md5( $this->input->post('repassword') );
					if( $admin_data['repassword'] != $admin_data['password'] )  throw new Exception( '两次密码不一致', -1 );
				}
				
				if($this->input->post('begin_time')){
					$admin_data['begin_time'] = strtotime($this->input->post('begin_time'));
				}
				if($this->input->post('begin_time')){
					$admin_data['end_time'] = strtotime($this->input->post('begin_time'));
				}
				
				//修改
				$this->admin_model->update_by_pk( $admin_data ,$id);
				init_messagebox( '修改成功', 'success', 1, base_url( $this->_site_path.'/admin/index' ) );
			} catch ( Exception $e ) {
				init_messagebox( $e->getMessage(), 'error', $e->getCode() );
			}
		}
		$this->_init_data( $data );
		$this->load->view($this->_site_path.'/admin/form', $data );
	}	

	/**
	 * delete 删除
	 * @param $id 主键值
	 * @author 
	 */
	public function delete( $id ) {
		$this->admin_model->delete_by_pk( $id );
		redirect( base_url($this->_site_path.'/admin/index' ) );
	}

	/**
	 * delete_all 批量删除
	 * @param $id 主键值
	 * @author 
	 */
	public function delete_all() {
		$ids=$this->input->post('ids');
		foreach($ids as $id){
			$this->admin_model->delete_by_pk( $id );
		}
		redirect( base_url($this->_site_path.'/admin/index' ) );
	}
}
